﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using Web.SystemConfig.Services;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Models;
using DreamBuilder.Core.Container;
using System.Data;
using System.Collections;

namespace Web.Views.ArchiveManage
{
    public partial class PageFileList : PageBase
    {
        #region 属性变量
        [Dependency]
        public ICodeTable CodeTableService
        {
            get;
            set;
        }

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        [Dependency]
        public IPageFileService PageFileService
        {
            get;
            set;
        }

        [Dependency]
        public IAuth_Org orgService
        {
            get;
            set;
        }

        public string archiveTypeName;

        /// <summary>
        /// 档案类型
        /// </summary>
        public string ReqArchiveType
        {
            get
            {
                if (this.Request.QueryString["archiveType"] != null)
                {
                    return this.Request.QueryString["archiveType"];
                }
                else
                {
                    throw new Exception("档案类型不能为空");
                }
            }
        }

        #endregion

        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                ArchiveMangeModal.ARCHIVEMENUDataTable dt = ArchiveMenuService.GetArchiveMenuById(ReqArchiveType);
                ArchiveMangeModal.ARCHIVEMENURow row = dt.Rows[0] as ArchiveMangeModal.ARCHIVEMENURow;
                archiveTypeName = row.ARCHIVEMENU;
            }
        }

        private DataTable GetArchivePageFile()
        {
            DataTable dtPageFile = null;
            IPageFileService pagefile = UContainer.Resolve<IPageFileService>();
            DataTable dt2 = pagefile.GetPageFileByArchiveType(ReqArchiveType);
            if (dtPageFile != null)
            {
                dtPageFile.Merge(dt2);
            }
            else
            {
                dtPageFile = dt2;
            }
            return dtPageFile;
        }

        private void BindGrid()
        {
            int recordCount = 0;
            DataTable dt = GetArchivePageFile();
            this.gv_PageFile.DataSource = PageDataList.GetPagedTable(dt, pager.CurrentPageIndex, pager.PageSize,out recordCount);
            pager.RecordCount = recordCount;
            this.gv_PageFile.DataBind();
        }
        #endregion

        #region 页面事件
        protected void gv_pageFileRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                System.Web.UI.WebControls.GridViewRow rows = ((GridViewRow)e.Row);
                string DataID = drv["DataID"].ToString();
                //int orderNum = Convert.ToInt32(drv["ORDERNUM"]);
                string url = "PageFileEdit.aspx?archiveType=" + ReqArchiveType + "&DataID=" + DataID + "&ReadOnly=true";
                ((System.Web.UI.WebControls.Button)rows.FindControl("view")).Attributes["onclick"] = "javascript:openViewPage('" + url + "');return false;";
                e.Row.Cells[5].Text = CodeTableService.GetpkNameByCodepkValue(drv["F_SECURITYTYPE"].ToString());
                e.Row.Cells[6].Text = CodeTableService.GetpkNameByCodepkValue(drv["F_KEEPTIME"].ToString());
                //e.Row.Cells[7].Text = orgService.GetOrgNameByOrgID(drv["FIleOrgID"].ToString());
                e.Row.Cells[9].Text = CodeTableService.GetpkNameByCodepkValue(drv["F_CARRIERTYPE"].ToString());
            }
        }

        protected void pager_PageChanging(object sender, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pager.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }
        #endregion

        #region  按钮事件
        /// <summary>
        /// 选择卷内件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ArrayList keys = this.gv_PageFile.SelectItems;
            if (keys != null && keys.Count > 0)
            {
                //将选择的卷内件加入列表
                Hashtable ht = new Hashtable();
                foreach (string dataid in keys)
                {
                    ht.Add( dataid,ReqArchiveType);
                }
                Session["ArchivePageFile"] = ht;
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), "closePage();", true);
            }
            else
            {
                Alert("您未选中任何行");
            }
        }
        #endregion
    }
}